package com.myzl.web.controller.coal;

import com.myzl.coal.domain.RealTimeData;
import com.myzl.coal.domain.vo.ComparisonVO;
import com.myzl.coal.dto.CmdDTO;
import com.myzl.coal.service.IRealTimeDataService;
import com.myzl.coal.service.protocol.CmdService;
import com.myzl.common.annotation.Log;
import com.myzl.common.core.controller.BaseController;
import com.myzl.common.core.domain.AjaxResult;
import com.myzl.common.core.page.TableDataInfo;
import com.myzl.common.enums.BusinessType;
import com.myzl.common.enums.DeviceCommandEnum;
import com.myzl.common.enums.DeviceTypeEnum;
import com.myzl.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 实时数据Controller
 *
 * @author myzl
 * @date 2022-09-01
 */
@RestController
@RequestMapping("/coal/realtimeData")
public class RealtimeDataController extends BaseController {
    @Autowired
    private IRealTimeDataService realtimeDataService;
    @Autowired
    private CmdService cmdService;

    /**
     * 查询实时数据列表
     */
    @PreAuthorize("@ss.hasPermi('coal:realtimeData:list')")
    @GetMapping("/list")
    public TableDataInfo list(RealTimeData realtimeData) {
        startPage();
        List<RealTimeData> list = realtimeDataService.selectRealTimeDataList(realtimeData);
        return getDataTable(list);
    }

    /**
     * 导出实时数据列表
     */
    @PreAuthorize("@ss.hasPermi('coal:realtimeData:export')")
    @Log(title = "实时数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RealTimeData realtimeData) {
        List<RealTimeData> list = realtimeDataService.selectRealTimeDataList(realtimeData);
        ExcelUtil<RealTimeData> util = new ExcelUtil<RealTimeData>(RealTimeData.class);
        util.exportExcel(response, list, "实时数据数据");
    }

    /**
     * 获取实时数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('coal:realtimeData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(realtimeDataService.selectRealTimeDataById(id));
    }

    /**
     * 新增实时数据
     */
    @PreAuthorize("@ss.hasPermi('coal:realtimeData:add')")
    @Log(title = "实时数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RealTimeData realtimeData) {
        return toAjax(realtimeDataService.insertRealTimeData(realtimeData));
    }

    /**
     * 修改实时数据
     */
    @PreAuthorize("@ss.hasPermi('coal:realtimeData:edit')")
    @Log(title = "实时数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RealTimeData realtimeData) {
        return toAjax(realtimeDataService.updateRealTimeData(realtimeData));
    }

    /**
     * 删除实时数据
     */
    @PreAuthorize("@ss.hasPermi('coal:realtimeData:remove')")
    @Log(title = "实时数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(realtimeDataService.deleteRealTimeDataByIds(ids));
    }

    /**
     * 统计数据-对比分析
     */
    @PreAuthorize("@ss.hasPermi('coal:realtimeData:comparison')")
    @Log(title = "统计数据", businessType = BusinessType.OTHER)
    @GetMapping("/comparison/{deviceId}")
    public AjaxResult comparison(@PathVariable("deviceId") Integer deviceId) {
        ComparisonVO comparison = realtimeDataService.comparison(deviceId, "");
        return AjaxResult.success(comparison);
    }


    /**
     * 当前瞬时量
     */
    @Log(title = "当前瞬时量", businessType = BusinessType.OTHER)
    @GetMapping("/currentFlow/{deviceId}")
    public AjaxResult currentFlow(@PathVariable("deviceId") Integer deviceId) {
        RealTimeData realtimeData = realtimeDataService.currentFlow(deviceId);
        return AjaxResult.success(realtimeData);
    }

    /**
     * 当前班次设备折线图统计数据
     */
    @Log(title = "当前班次设备折线图统计数据", businessType = BusinessType.OTHER)
    @GetMapping("/brokenLineData/{deviceId}")
    public AjaxResult brokenLineData(@PathVariable("deviceId") Integer deviceId) {
        return realtimeDataService.brokenLineData(deviceId);
    }

    /**
     * 查询瞬时量，下发命令
     */
    @Log(title = "查询瞬时量", businessType = BusinessType.OTHER)
    @GetMapping("/flowCommand/{deviceId}")
    public AjaxResult flowCommand(@PathVariable("deviceId") Integer deviceId) {
        cmdService.read(new CmdDTO(DeviceTypeEnum.DOUBLE_ORDER.getCode(), deviceId, DeviceCommandEnum.DEVICE_CURRENT_VOLUME.getCode(), DeviceCommandEnum.DEVICE_CURRENT_VOLUME.getNumber()));
        return AjaxResult.success();
    }

    /**
     * 查询平均厚度
     */
    @Log(title = "查询平均厚度", businessType = BusinessType.OTHER)
    @GetMapping("/averageThickness/{deviceId}")
    public AjaxResult averageThickness(@PathVariable("deviceId") Integer deviceId) {
        return realtimeDataService.averageThickness(deviceId);
    }

    /**
     * 查询煤流当前截面图
     */
    @Log(title = "当前截面", businessType = BusinessType.OTHER)
    @GetMapping("/section/{deviceId}")
    public AjaxResult section(@PathVariable("deviceId") Integer deviceId) {
        return realtimeDataService.sectionData(deviceId);
    }

    /**
     * 查询30分钟煤流厚度趋势数据
     */
    @Log(title = "30分钟煤流厚度趋势数据", businessType = BusinessType.OTHER)
    @GetMapping("/trend/{deviceId}")
    public AjaxResult trend(@PathVariable("deviceId") Integer deviceId) {
        return realtimeDataService.trendData(deviceId);
    }

    /**
     * 查询30分钟筛机截图分类占比
     */
    @Log(title = "30分钟筛机截图分类占比", businessType = BusinessType.OTHER)
    @GetMapping("/clasProportion/{deviceId}")
    public AjaxResult clasProportion(@PathVariable("deviceId") Integer deviceId) {
        return realtimeDataService.proportionData(deviceId);
    }


}
